<?php
if (isset($_POST['cmbListaClientes'])) {
    $codigo = $_POST['cmbListaClientes'];
    $cliente = new Clientes();
    $cliente->Carrega($codigo);
}
?>
<form method="post" id="form_cliente" action="controller/acoes_cliente.php" >
    <input type="hidden" name="hdCodigo" id="hdCodigo" value="<?php
if (isset($_POST['cmbListaClientes'])) {
    echo $cliente->getCodigo();
}
?>" />
    <table>
        <tr>
            <td><span class="campoRequerido">*</span><input type="text" name="txtNomeCliente" id="txtNomeCliente" placeholder="Nome" class="campo" value="<?php if (isset($_POST['cmbListaClientes'])) { echo $cliente->getNome(); }?>" />

                <!--<input type="button" name="btBuscar" id="btBuscar" onclick="carregaBusca('DivPesquisaCliente', 'ajax/form_busca_clientes.php');" />-->
            </td>
        </tr>
        <tr>
            <td><hr size="1" color="#000000" /></td>
        </tr>
        <tr>
            <td><input type="text" name="txtCodPosto" id="txtCodPosto" class="campo" placeholder="C&oacute;digo do Posto" value="<?php
                       if (isset($_POST['cmbListaClientes'])) {
                           echo $cliente->getCodigodoposto();
                       }
?>" /></td>
        </tr>
        <tr>
            <td><hr size="1" color="#000000" /></td>
        </tr>
        <tr>
            <td><span class="campoRequerido">*</span><input type="text" name="txtBandeira" id="txtBandeira" class="campo" placeholder="Bandeira" value="<?php
                       if (isset($_POST['cmbListaClientes'])) {
                           echo $cliente->getBandeira();
                       }
?>" /></td>
        </tr>
        <tr>
            <td><hr size="1" color="#000000" /></td>
        </tr>
        <tr>
            <td>
                <?php
                if (isset($_POST['cmbListaClientes'])) {
                    $listaUfSelecionado = Municipios::listaUfMunicipio(" WHERE codigo = '{$cliente->getCodmunicipio()}' ");
                    foreach ($listaUfSelecionado as $lista) {
                        $uf = $lista->getUf();
                    }
                }
                $listaUF = Municipios::listaUfMunicipio(" GROUP BY uf ORDER BY uf ");
                ?>
                <font face="Trebuchet MS, Arial, Helvetica, sans-serif" color="#FFFFFF" size="+2" >UF<span class="campoRequerido">*</span>:</font> <select class="campo" name="cmbUf" id="cmbUf" onchange="ListaMunicipios(this)">
                    <option value="" ></option>
                    <?php
                    foreach ($listaUF as $key) {
                        ?>
                        <option value="<?php echo $key->getUf(); ?>" <?php
                    if (isset($uf)) {
                        if ($uf == $key->getUf()) {
                            echo "selected=\"selected\"";
                        }
                    }
                        ?>><?php echo $key->getUf(); ?></option>
                                <?php
                            }
                            ?>
                </select>
            </td>
        </tr>
        <tr>
            <td><hr size="1" color="#000000" /></td>
        </tr>
        <tr>
            <td id="tdRetorno">
                <?php
                if (isset($uf)) {
                    $listaMunicipios = Municipios::listaUfMunicipio(" WHERE uf = '$uf' ORDER BY nome");
                    ?>
                <font face="Trebuchet MS, Arial, Helvetica, sans-serif" color="#FFFFFF" size="+2" >Munic&iacute;pios:</font> <select  class="campo" name="cbMunicipios" id="cbMunicipios" onchange="ListaPostosdoMunicipio(this)">
                        <option value=""></option>
                        <?php
                        foreach ($listaMunicipios as $dados) {
                            ?>
                            <option value="<?php echo $dados->getCodigo(); ?>" <?php
                    if (isset($uf)) {
                        if ($cliente->getCodmunicipio() == $dados->getCodigo()) {
                            echo "selected=\"selected\"";
                        }
                    }
                            ?>><?php echo $dados->getNome(); ?></option>
                                    <?php
                                }
                                ?>
                    </select>
                    <?php
                } else {
                    ?>
                    <font face="Trebuchet MS, Arial, Helvetica, sans-serif" color="#FFFFFF" size="+2" >Munic&iacute;pios:</font> <select class="campo" name="cbMunicipios" id="cbMunicipios"></select>
                    <?php
                }
                ?>
            </td>
        </tr>
        <tr>
            <td><hr size="1" color="#000000" /></td>
        </tr>
        <tr>
            <td><input type="text" name="txtVolume" id="txtVolume" placeholder="Volume" class="campo" value="<?php if (isset($_POST['cmbListaClientes'])) { echo $cliente->getVolume(); }?>" /></td>
        </tr>
        <tr>
            <td><hr size="1" color="#000000" /></td>
        </tr>
        <tr>
            <td><input type="text" name="txtNomeOperador" id="txtNomeOperador" placeholder="Nome Operador" class="campo" value="<?php if (isset($_POST['cmbListaClientes'])) { echo $cliente->getNome_operador(); }?>" /></td>
        </tr>
        <tr>
            <td><hr size="1" color="#000000" /></td>
        </tr>
        <tr>
            <td><input type="text" name="txtTelefone" id="txtTelefone" placeholder="N&deg; Telefone" class="campo" value="<?php if (isset($_POST['cmbListaClientes'])) { echo $cliente->getTelefone(); }?>" /></td>
        </tr>
        <tr>
            <td><hr size="1" color="#000000" /></td>
        </tr>
        <tr>
            <td><input type="text" name="txtDataVencimentoContrato" id="txtDataVencimentoContrato" placeholder="Venc. do Contrato" class="campo" value="<?php if (isset($_POST['cmbListaClientes'])) { echo $cliente->getContrato_vencimento(); }?>" /></td>
        </tr>
        <tr>
            <td><hr size="1" color="#000000" /></td>
        </tr>
        <tr>
            <td>
                <?php
                $tipoAssessor = Util::retornaTipo("Assessor");
                if($nivel <= 1){
                    $strWhere = " AND codigo = '$logado'";
                }else if($nivel < 3){
                    $strWhere = " AND codsupervisor = '$logado'";
                }else{
                    $strWhere = "";
                }
                
                $listaAssessores = Usuarios::Lista_usuario(" WHERE codtipo = '$tipoAssessor' $strWhere ORDER BY nome ");
                ?>
                <select name="cmbAssessor" id="cmbAssessor" class="campo">
                    <?php
                    foreach($listaAssessores as $lista){
                    ?>
                        <option value="<?php echo $lista->getCodigo();?>" 
                            <?php 
                                if (isset($_POST['cmbLista'])) {
                                    if ($cliente->getCodusuario() == $lista->getCodigo()) {
                                        echo "selected=\"selected\"";
                                    }
                                }
                            ?>><?php echo $lista->getNome();?></option>
                    <?php
                    }
                    ?>
                </select>
            </td>
        </tr>
        <tr>
            <td>
                <select class="campo" name="cbEstado" id="cbEstado">
                    <option value="A" <?php
                if (isset($_POST['cmbListaClientes'])) {
                    if ($cliente->getEstado() == "A") {
                        echo "selected=\"selected\"";
                    }
                }
                ?>>Ativo</option>
                    <option value="I" <?php
                            if (isset($_POST['cmbListaClientes'])) {
                                if ($cliente->getEstado() == "I") {
                                    echo "selected=\"selected\"";
                                }
                            }
                ?>>Inativo</option>
                </select>
            </td>
        </tr>
        <tr>
            <td><hr size="1" color="#000000" /></td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="btSalvar" id="btSalvar" class="botao2" value="Salvar" onclick="return ValidaFormulario('txtNomeCliente|txtBandeira|cmbUf|cbMunicipios','Preencha os campos requeridos')" />
                <input type="submit" name="btDeletar" id="btDeletar" class="botao2" value="Deletar" />
            </td>
        </tr>
        <tr>
            <td>&nbsp;</td>
        </tr>
    </table>
</form>